//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderError;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.utils.StringUtils;

	public class StartUpCommand extends SimpleCommand implements ICommand
	{
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		private var airReader:Reader;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		override public function execute(notification:INotification):void
		{
			airReader = notification.getBody() as Reader;
			
			//let ReaderApplication represent Reader instance
			readerModelLocator.airReader = airReader;
			
			//loading resource files 
			var file:File = File.applicationDirectory.resolvePath(ReaderModelLocator.CONFIG_PATH);
			if(!file.exists)
				throw new ReaderError(ReaderError.CONFIG_ERROR);
				
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var config:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
			config = StringUtils.trimAll(config);
			var result:Array = config.split(";");
			var obj:Object = {};
			for each(var i:String in result)
			{
				var a:Array = i.split("=");
				obj[a[0]] = a[1];
			}
			fileStream.close();
			
			loadSubscribeConfig();
			
			if(!obj["Language"])
				throw new ReaderError(ReaderError.CONFIG_ERROR);
			ReaderModelLocator.instance.updatePath = obj["Update"];
			ReaderModelLocator.instance.onlineHelp = obj["OnlineHelp"];
			loadResource(obj["Language"]);
		}
		
		private function loadResource(culture:String):void
		{
			ReaderModelLocator.instance.culture = culture;
			var file:File = File.applicationDirectory.resolvePath("resource/" + culture + ".res");
			if(!file.exists)
				throw new ReaderError(ReaderError.RESOURCE_ERROR);
				
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			ReaderResource.instance.resource = new XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			fileStream.close();
			
			facade.registerProxy(ReaderModelLocator.instance.sqliteProxyFactory.dataProxy);
			facade.registerProxy(ReaderModelLocator.instance.sqliteProxyFactory.serviceProxy);
			
			sendNotification(ApplicationFacade.UPDATE_READER);
			sendNotification(ApplicationFacade.RESOURCE_LOAD, airReader);
		}
		
		private function loadSubscribeConfig():void
		{
			var file:File = File.applicationDirectory.resolvePath(ReaderModelLocator.FEED_CONFIG_PATH);
			if(!file.exists)
				throw new ReaderError(ReaderError.SUBSCRIBE_CONFIG_ERROR);
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			var xml:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
			ReaderModelLocator.instance.subscribeConfig = new XML(xml);
			fileStream.close();
		}
	}
}